#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from fastapi import APIRouter, Depends, Request, Response, UploadFile
from fastapi.security import OAuth2PasswordRequestForm
from loguru import logger

from app.api import jwt
from app.api.service import user_service
from app.common.pagination import Page
from app.common.response.response_schema import response_base
from app.models import User
from app.schemas.token import LoginFail, Token
from app.schemas.user import Auth, CreateUser, GetUserInfo, ResetPassword, UpdateUser, ELCode, Auth2

user_router = APIRouter()


@user_router.post('/login', summary='表单登录', response_model=Token, description='form 格式登录支持直接在 api 文档调试接口')
async def user_login(form_data: OAuth2PasswordRequestForm = Depends()):
    token  = await user_service.login(form_data)
    return Token(access_token=token)



@user_router.post('/loginjson', summary='用户登录', response_model=Token,description='json 格式登录, 不支持api文档接口调试, 需使用第三方api工具, 例如: postman')
async def user_login(obj: Auth):
    token  = await user_service.login(obj)
    return Token(access_token=token)
    



@user_router.post('/logout', summary='用户退出', dependencies=[Depends(jwt.get_current_user)])
async def user_logout():
    return response_base.response_200(msg='退出登录成功')


@user_router.get('/', summary='查看用户信息', dependencies=[Depends(jwt.get_current_user)])
async def get_userinfo(current_user: User = Depends(jwt.get_current_user)):
    user_dict = current_user.__dict__
    user_dict.update({
        "photo" : f'http://aushrap3/AUOPhoneBookPhoto/ks_photo/{current_user.emp_no}.jpg'
        # "photo" : f'https://pica.zhimg.com/80/v2-108785abb6db4a853e03480da12ae900_720w.webp?source=1940ef5c'
    })
    return response_base.response_200(
        msg='查看用户信息成功',
        data=user_dict,
        exclude={'password','_sa_instance_state'}
    )


@user_router.get('/all', summary='查看所有用户的信息', dependencies=[Depends(jwt.get_current_user)])
async def get_userinfos(current_user: User = Depends(jwt.get_current_user)):
    res = await user_service.get_users(current_user)
    return response_base.response_200(
        msg='查看用户信息成功',
        data=res,
    )







@user_router.get('/{username}', summary='查看用户信息', dependencies=[Depends(jwt.get_current_user)])
async def get_userinfo(username: str):
    current_user = await user_service.get_user_info(username)
    return response_base.response_200(
        msg='查看用户信息成功',
        data=current_user,
        exclude={'password'}
    )


